package com.ignoreexception.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.List;
import java.util.Properties;

/**
 * 后面还会做一些和SpringMVC相关的配置
 * 需要实现接口 WebMvcConfigurer，如果实现接口里的方法就是提供一些额外的配置信息
 * @since 2022/12/26 19:58
 */
//@Configuration
@ComponentScan("com.ignoreexception.controller")
@EnableWebMvc // mvc:annotation-driven
public class WebMvcServletConfiguration implements WebMvcConfigurer {
    /**
     * 添加Interceptors拦截器
     * @since 2022/12/26 19:59
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInterceptors()).addPathPatterns("/user/hello");
    }

    //设置静态资源路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //web资源路径
        //classpath:/类加载路径
        //file:文件路径/  建议使用 file:D:/picture/
        registry.addResourceHandler("/pic/**").addResourceLocations("/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:D:/picture/");
    }

    //处理静态资源
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //视图控制器
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    //    @Bean
//    //文件上传解析器
//    //@Bean 可以把表示的方法的返回值作为Bean进行管理,就会被配置到IOC容器中作为一个bean进行管理
//    // Bean的ID为方法的方法名
//    public CommonsMultipartResolver multipartResolver() {
//        return new CommonsMultipartResolver();
//    }
    @Override
    //配置异常解析器
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
        Properties prop = new Properties();
        prop.setProperty("java.lang.ArithmeticException", "error");
        //设置异常映射
        exceptionResolver.setExceptionMappings(prop);
        //设置共享异常信息的键
        exceptionResolver.setExceptionAttribute("ex");
        resolvers.add(exceptionResolver);
    }

//    //配置生成模板解析器
//    @Bean
//    public ITemplateResolver templateResolver() {
//        WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
//        // ServletContextTemplateResolver需要一个ServletContext作为构造参数，可通过WebApplicationContext 的方法获得
//        ServletContextTemplateResolver templateResolver = new
//                ServletContextTemplateResolver(webApplicationContext.getServletContext());
//        templateResolver.setPrefix("/WEB-INF/templates/");
//        templateResolver.setSuffix(".html");
//        templateResolver.setCharacterEncoding("UTF-8");
//        templateResolver.setTemplateMode(TemplateMode.HTML);
//        return templateResolver;
//    }
//
//    //生成模板引擎并为模板引擎注入模板解析器
//    @Bean
//    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
//        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(templateResolver);
//        return templateEngine;
//    }
//
//    //生成视图解析器并未解析器注入模板引擎
//    @Bean
//    public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
//        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
//        viewResolver.setCharacterEncoding("UTF-8");
//        viewResolver.setTemplateEngine(templateEngine);
//        return viewResolver;
//    }
}
